/**
 * 
 */
package zkoran.qrfan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Activity to create a new match
 * 
 * @author zdenda
 * 
 */
public class NewMatchActivity extends Activity {

	private EditText mEditTextName;
	private Button mButtonOk;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_match);

		mEditTextName = (EditText) findViewById(R.id.editText_name);

		mButtonOk = (Button) findViewById(R.id.button_ok);
		mButtonOk.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				createNewMatch();

			}
		});

	}

	/**
	 * Create new match and finishes this activity
	 */
	protected void createNewMatch() {

		String name = mEditTextName.getText().toString().trim();

		// if name is valid
		if (!MatchModel.validName(name).equals("")) {
			
			// if match was created, we can finish this activity
			if (MatchModel.createMatch(name)) {

				Intent intent = new Intent();
				this.setResult(RESULT_OK, intent);
				this.finish();

			} else {
				Toast.makeText(this, R.string.message_file_error,
						Toast.LENGTH_LONG).show();
			}

		} else {
			Toast.makeText(this, R.string.message_empty_name, Toast.LENGTH_LONG).show();
		}

	}

}
